import time
import uuid
import random
import string
import qrcode
from io import BytesIO
from datetime import datetime
from base64 import b64encode

def generate_order_no():
    """生成订单编号"""
    # 格式: 年月日时分秒 + 6位随机数
    now = datetime.now()
    date_str = now.strftime('%Y%m%d%H%M%S')
    random_str = ''.join(random.choices(string.digits, k=6))
    return f"ORD{date_str}{random_str}"

def generate_payment_no():
    """生成支付流水号"""
    # 格式: 年月日时分秒 + 6位随机数
    now = datetime.now()
    date_str = now.strftime('%Y%m%d%H%M%S')
    random_str = ''.join(random.choices(string.digits, k=6))
    return f"PAY{date_str}{random_str}"

def generate_checkin_code():
    """生成签到码"""
    # 生成8位随机字母数字组合
    chars = string.ascii_uppercase + string.digits
    return ''.join(random.choices(chars, k=8))

def generate_qrcode(data):
    """生成二维码"""
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    qr.add_data(data)
    qr.make(fit=True)
    
    img = qr.make_image(fill_color="black", back_color="white")
    
    # 将图片转换为base64
    buffered = BytesIO()
    img.save(buffered, format="PNG")
    img_str = b64encode(buffered.getvalue()).decode()
    
    return f"data:image/png;base64,{img_str}" 